import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../res/colors.dart';
import '../../util/toast_util.dart';
import '../../widget/keep_alive_wrapper.dart';
import 'home_logic.dart';

class HomePage extends StatelessWidget {
  HomePage({Key? key}) : super(key: key);

  final logic = Get.find<HomeLogic>();
  final state = Get.find<HomeLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Obx(() => WillPopScope(
        child: Scaffold(
          backgroundColor: Colors.transparent, // 设置背景为透明
          body: KeepAliveWrapper(
            child: PageView(
              controller: state.pageController,
              physics: const NeverScrollableScrollPhysics(),
              children: state.naviItems,
            ),
          ),
          bottomNavigationBar: BottomNavigationBar(
            elevation: 8.0,
            type: BottomNavigationBarType.fixed,
            currentIndex: state.curPage.value,
            items: state.navigationViews.map((view) => view.item).toList(),
            fixedColor: ColorStyle.color_FFF06100,
            onTap: (int index) {
              ///跳转到指定页面
              state.pageController.jumpToPage(index);
              state.curPage.value = index;
            },
          ),
        ),
        onWillPop: () async {
          if (state.lastPopTime == null || DateTime.now().difference(state.lastPopTime!) > const Duration(seconds: 2)) {
            // 存储当前按下back键的时间
            state.lastPopTime = DateTime.now();
            // toast
            ToastUtil.showTip("再按一次退出app");
            return false;
          } else {
            state.lastPopTime = DateTime.now();
            // 退出app
            await SystemNavigator.pop();
            return true;
          }
        }));
  }
}
